using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;

public partial class admin_frmUsersAddModify : System.Web.UI.Page
{
    clsUser clsUser;
    clsUsersRecord clsUsersRecord;

    #region EVENT METHODS

    protected void Page_Load(object sender, EventArgs e)
    {
        //### Check if session clsUser exists
        if (Session["clsUser"] == null)
        {
            {
                //### Redirect back to login
                Response.Redirect("../frmAdminLogin.aspx");
            }
        }

        clsUser = (clsUser)Session["clsUser"];

        if (!IsPostBack)
        {
            //### If the UserID is passed through then we want to instantiate the obect with that iUserID
            if (Request.QueryString["iUserID"] != "" && Request.QueryString["iUserID"] != null)
            {
                //## Set label to modify
                lblAddModify.Text = "Modify User";

                clsUsersRecord = new clsUsersRecord(clsUser, Convert.ToInt32(Request.QueryString["iUserID"]));

                //### Populate the form
                popFormData();

                //### Hide password rows if editing
                if (Request.QueryString["action"] == "edit")
                {
                    passField.Attributes.Add("style", "display:none");
                    passLabel.Attributes.Add("style", "display:none");
                    confirmPassField.Attributes.Add("style", "display:none");
                    confirmPassLabel.Attributes.Add("style", "display:none");
                }
            }
            else
            {
                //### Set label to add
                lblAddModify.Text = "Add User";

                clsUsersRecord = new clsUsersRecord(clsUser);
            }
            Session["clsUsersRecord"] = clsUsersRecord;
        }
        else
        {
            clsUsersRecord = (clsUsersRecord)Session["clsUsersRecord"];
        }

        Title = lblAddModify.Text;
    }

    protected void btnBack_Click(object sender, ImageClickEventArgs e)
    {
        //### Go back to previous page
        Response.Redirect("frmUsersView.aspx");
    }

    protected void btnSave_Click(object sender, ImageClickEventArgs e)
    {
        //### Validate registration process
        bool bCanSave = true;

        bCanSave = clsValidation.IsNullOrEmpty(txtFirstName, reqFirstName, bCanSave);
        bCanSave = clsValidation.IsNullOrEmpty(txtSurname, reqSurname, bCanSave);
        bCanSave = clsValidation.IsNullOrEmpty(txtEmail, reqEmail, bCanSave);

        //### Hide password rows if editing
        if (Request.QueryString["action"] != "edit")
        {
            bCanSave = clsValidation.IsNullOrEmpty(txtPassword, reqPassword, bCanSave);
            bCanSave = clsValidation.IsNullOrEmpty(txtConfirmPassword, reqConfirmPassword, bCanSave);
        }

        if (!string.IsNullOrEmpty(txtEmail.Text))
        {
            //### Check to see if the username exists
            if (clsCommonFunctions.DoesRecordExist("tblUsers", "strEmailAddress LIKE '%" + txtEmail.Text + "%' AND bIsActive = 1") && Request.QueryString["action"] != "edit")
            {
                bCanSave = false;
                reqUserExists.Attributes.Add("style", "display:block");
                txtEmail.Attributes.Add("style", "border:1px solid #911f22;");
            }
            else
            {
                reqUserExists.Attributes.Add("style", "display:none;");
                txtEmail.Attributes.Add("style", "border:1px solid #999;");

            }
        }

        if (bCanSave == true)
        {
            SaveData();
        }
        else
        {
            reqContainerDiv.Visible = true;
        }
    }

    protected void btnClear_Click(object sender, ImageClickEventArgs e)
    {
        reqContainerDiv.Visible = false;

        txtFirstName.Text = "";
        clsValidation.SetValid(txtFirstName, reqFirstName);

        txtSurname.Text = "";
        clsValidation.SetValid(txtSurname, reqSurname);

        txtEmail.Text = "";
        clsValidation.SetValid(txtEmail, reqEmail);

        txtPassword.Text = "";
        clsValidation.SetValid(txtPassword, reqPassword);

        txtConfirmPassword.Text = "";
        clsValidation.SetValid(txtConfirmPassword, reqConfirmPassword);
    }

    #endregion

    #region POPULATE DATA METHODS

    private void popFormData()
    {
        txtFirstName.Text = clsUsersRecord.strFirstName;
        txtSurname.Text = clsUsersRecord.strSurname;
        txtEmail.Text = clsUsersRecord.strEmailAddress;
    }

    #endregion

    #region SAVE DATA METHODS

    private void SaveData()
    {
        //### Hash the password
        string strHashPassword = clsCommonFunctions.GetMd5Sum(txtPassword.Text);

        //### Only update the password if there is a value
        if (txtPassword.Text != "")
        {
            clsUsersRecord.strPassword = strHashPassword;
        }

        //### Add / Update
        clsUsersRecord.dtAdded = Convert.ToDateTime(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));
        clsUsersRecord.iAddedBy = clsUser.iUserID;
        clsUsersRecord.dtEdited = Convert.ToDateTime(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));
        clsUsersRecord.iEditedBy = clsUser.iUserID;
        clsUsersRecord.strFirstName = txtFirstName.Text;
        clsUsersRecord.strSurname = txtSurname.Text;
        clsUsersRecord.strEmailAddress = txtEmail.Text;
        clsUsersRecord.bIsActive = true;

        clsUsersRecord.Update();

        //### redirect back to view page
        Response.Redirect("frmUsersView.aspx");

    }

    #endregion
}
